#include "postchangeinfomange.h"
#include "ui_postchangeinfomange.h"
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QSqlRecord>
#include <QDebug>

PostChangeInfoMange::PostChangeInfoMange(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PostChangeInfoMange)
{
    ui->setupUi(this);
    this->setWindowTitle("干部职务变动信息管理");
    this->setGeometry(300,200,1000,800);
    initTableWidget();
    initModelView();
}

PostChangeInfoMange::~PostChangeInfoMange()
{
    delete ui;
}

//初始化tableWidget
void PostChangeInfoMange::initTableWidget()
{
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->horizontalHeader()->resizeSection(1,150);

    //显示职级对照表
    QSqlQuery query;
    query.exec("select RankID,RankName from RankCode");
    while(query.next()) //一行一行遍历
    {
        int rowc = ui->tableWidget->rowCount();//获取行数
        ui->tableWidget->insertRow(rowc);
        ui->tableWidget->setItem(rowc,0,new QTableWidgetItem(query.value(0).toString()));
        ui->tableWidget->setItem(rowc,1,new QTableWidgetItem(query.value(1).toString()));

    }

}

//初始化显示数据库post表
void PostChangeInfoMange::initModelView()
{
    ui->tableView->clearSpans();
    model  = new QSqlTableModel(this);
    model->setTable("Post");//制定使用哪个表

    //把model放在view
    ui->tableView->setModel(model);

    //显示model里的数据
    model->select();

    model->setHeaderData(0, Qt::Horizontal, "职务ID");
    model->setHeaderData(1, Qt::Horizontal, "职务名称");
    model->setHeaderData(2, Qt::Horizontal, "职级ID");
    model->setHeaderData(3, Qt::Horizontal, "职务属性");
    model->setHeaderData(4, Qt::Horizontal, "职务说明");
    model->setHeaderData(5, Qt::Horizontal, "任免类型");
    model->setHeaderData(6, Qt::Horizontal, "任职性质");
    model->setHeaderData(7, Qt::Horizontal, "任免时间");
    model->setHeaderData(8, Qt::Horizontal, "任免文号");
    model->setHeaderData(9, Qt::Horizontal, "证件号码");
    model->setHeaderData(10, Qt::Horizontal, "单位");

    //设置model的编辑模式，手动提交修改
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
}

//添加记录
void PostChangeInfoMange::on_buttonAdd_clicked()
{
    //添加空记录
    QSqlRecord record = model->record(); //获取空记录
    //获取行号
    int row = model->rowCount();
    model->insertRecord(row, record);
}

//删除记录
void PostChangeInfoMange::on_buttonDel_clicked()
{
    //获取选中的模型
    QItemSelectionModel *sModel =ui->tableView->selectionModel();
    //取出模型中的索引
    QModelIndexList list = sModel->selectedRows();
    //删除所有选中的行
    for(int i = 0; i < list.size(); i++)
    {
        model->removeRow( list.at(i).row() );
    }
}

//确定，提交动作
void PostChangeInfoMange::on_buttonSure_clicked()
{
    model->submitAll(); //提交动作
}

//取消，回滚
void PostChangeInfoMange::on_buttonCancel_clicked()
{
    model->revertAll(); //取消所用动作
    model->submitAll(); //提交动作
}

//查询
void PostChangeInfoMange::on_buttonFind_clicked()
{
    QString idNum = ui->lineEdit->text();
    QString str = QString("IDNum = '%1'").arg(idNum);
    model->setFilter(str);
    model->select();
}

//返回最初显示
void PostChangeInfoMange::on_buttonBack_clicked()
{
    initModelView();
}
